# Check instances where segment/link made up of more than 1 line ---------------
which(endpoints$n_seg > 1)
# Check instances where segment/link made up of more than 1 line ---------------
which(endpoints$n_seg > 1)
i <- 26
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
which(endpoints$n_seg > 1)
i <- 26
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
which(endpoints$n_seg > 1)
i <- 155
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
which(endpoints$n_seg > 1)
i <- 156
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
which(endpoints$n_seg > 1)
i <- 975
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
which(endpoints$n_seg > 1)
i <- 975
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 975
plot(roads_sdf_p123[i,])
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 13
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 78
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Separate into Phases ---------------------------------------------------------
roads_sdf_p123 <- roads_sdf[roads_sdf$Complete_G %in% 1997:2009,]
extract_line_endpoints_roadi <- function(i, road){
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road[i,])[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X2_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X2_diff_median < 0){
# Reverse order of dataframe
points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
}) %>%
bind_rows()
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(1:nrow(roads_sdf_p123), extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
extract_line_endpoints_roadi <- function(i, road){
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road[i,])[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X2_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X2_diff_median < 0){
# Reverse order of dataframe
#points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
}) %>%
bind_rows()
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(1:nrow(roads_sdf_p123), extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
extract_line_endpoints_roadi <- function(i, road){
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road[i,])[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X1 %>% diff() %>% median()
if(X1_diff_median < 0){
# Reverse order of dataframe
points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
}) %>%
bind_rows()
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(1:nrow(roads_sdf_p123), extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
i=488
plot(road[i,])
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road[i,])[[1]] %>%
lapply(as.data.frame)
length(points_list)
road_i <- road[i,]
road_i <- road[i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
road_i$LINKNAME
# Separate into Phases ---------------------------------------------------------
roads_sdf_p123 <- roads_sdf[roads_sdf$Complete_G %in% 1997:2009,]
extract_line_endpoints_roadi <- function(i, road){
road_i <- road[i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Made up of 3 segments, only use ones at ends
if(road_i$LINKNAME %in% "Kebribeyah-Hatrishek-Aware"){
points_list <- points_list[c(1,3),]
}
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X1 %>% diff() %>% median()
if(X1_diff_median < 0){
# Reverse order of dataframe
points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
}) %>%
bind_rows()
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(1:nrow(roads_sdf_p123), extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
extract_line_endpoints_roadi <- function(i, road){
road_i <- road[i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Made up of 3 segments, only use ones at ends
if(road_i$LINKNAME %in% "Kebribeyah-Hatrishek-Aware"){
points_list <- points_list[c(1,3),]
}
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0){
# Reverse order of dataframe
points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
}) %>%
bind_rows()
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(1:nrow(roads_sdf_p123), extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i=488
road_i <- road[i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
road_i$LINKNAME
c(1,3)
points_list
points_list[[-2]]
extract_line_endpoints_roadi <- function(i, road){
road_i <- road[i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Made up of 3 segments, only use ones at ends
if(road_i$LINKNAME %in% "Kebribeyah-Hatrishek-Aware"){
points_list[[2]] <- NULL
}
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0){
# Reverse order of dataframe
points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
}) %>%
bind_rows()
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(1:nrow(roads_sdf_p123), extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
# Check instances where segment/link made up of more than 1 line ---------------
endpoints$id[endpoints$n_seg > 1] %>% unique()
i <- 488
plot(roads_sdf_p123[i,])
plot(endpoints[endpoints$id %in% i,], add=T, col="red", pch=16)
head(road_i)
nrow(road_i)
road_i <- road[i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points_list <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0){
# Reverse order of dataframe
points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
})
rev_df <- function(df) df[dim(df)[1]:1,]
i=1
road_i <- road[i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points_list <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0){
# Reverse order of dataframe
points_list_i <- points_list_i[dim(points_list_i)[1]:1,]
}
return(points_list_i)
})
head(roads_sdf)
roads_sdf$LINKNAME
# Separate into Phases ---------------------------------------------------------
roads_sdf_p123 <- roads_sdf[roads_sdf$Complete_G %in% 1997:2009,]
roads_sdf_p123 <- raster::aggregate(roads_sdf_p123, by = "LINKNAME")
# Separate into Phases ---------------------------------------------------------
roads_sdf_p123 <- roads_sdf[roads_sdf$Complete_G %in% 1997:2009,]
roads_sdf_p123 <- raster::aggregate(roads_sdf_p123, by = "LINKNAME")
help(aggregate)
roads_sdf_p123$LINKNAME
source('~/Documents/Github/Ethiopia-Corridors-IE/DataWork/RSDP Roads/02_phase_123_roads_endpoints.R')
roads_sdf_p123$LINKNAME %>% length()
roads_sdf_p123$LINKNAME %>% length()
roads_sdf_p123$LINKNAME %>% unique() %>% length()
roads_sdf_p123$LINKNAME %>% unique() %>% length()
# Separate into Phases ---------------------------------------------------------
roads_sdf_p123 <- roads_sdf[roads_sdf$Complete_G %in% 1997:2009,]
extract_line_endpoints_roadi <- function(LINKNAME_i, road){
road_i <- road[road$LINKNAME %in% LINKNAME_i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points_list <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0) points_list_i <- rev_df(points_list_i)
return(points_list_i)
})
# Manually adjust select segments
# Made up of 3 segments, only use ones at ends
if(road_i$LINKNAME %in% "Kebribeyah-Hatrishek-Aware"){
points_list[[2]] <- NULL
}
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
extract_line_endpoints_roadi <- function(LINKNAME_i, road){
road_i <- road[road$LINKNAME %in% LINKNAME_i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points_list <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0) points_list_i <- rev_df(points_list_i)
return(points_list_i)
})
# Manually adjust select segments
# Made up of 3 segments, only use ones at ends
if(road_i$LINKNAME %in% "Kebribeyah-Hatrishek-Aware"){
points_list[[2]] <- NULL
}
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(unique(roads_sdf_p123$LINKNAME),
extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
LINKNAME_i <- "Kebribeyah-Hatrishek-Aware"
road_i <- road[road$LINKNAME %in% LINKNAME_i,]
road_i
plot(road_i)
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points_list <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0) points_list_i <- rev_df(points_list_i)
return(points_list_i)
})
length(points_list)
points_list[[2]] <- rev_df(points_list[[2]])
extract_line_endpoints_roadi <- function(LINKNAME_i, road){
road_i <- road[road$LINKNAME %in% LINKNAME_i,]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
# Within each dataframe, sort so that longitude is assending. First, calculate
# difference. If mostly negative, then reverse. This ensures (kinda) that each segment
# is in the same order.
points_list <- lapply(points_list, function(points_list_i){
names(points_list_i) <- c("X1", "X2")
X1_diff_median <- points_list_i$X2 %>% diff() %>% median()
if(X1_diff_median < 0) points_list_i <- rev_df(points_list_i)
return(points_list_i)
})
# Manually adjust select segments
# Made up of 3 segments, only use ones at ends
if("Kebribeyah-Hatrishek-Aware" %in% road_i$LINKNAME){
points_list[[2]] <- rev_df(points_list[[2]])
}
## Grab endpoints
points_end <- bind_rows(
points %>% head(1),
points %>% tail(1)
)
points_end$n_seg <- length(points_list)
points_end$id <- i
coordinates(points_end) <- ~X1+X2
crs(points_end) <- crs(road)
return(points_end)
}
endpoints <- lapply(unique(roads_sdf_p123$LINKNAME),
extract_line_endpoints_roadi, roads_sdf_p123) %>%
do.call(what = "rbind")
road_i[1:2,]
road
road_i <- road[1:2,]
coordinates(road_i)[[1]]
coordinates(road_i)[[1]] %>% length()
road_i <- road[1:2,]
head(road_i)
coordinates(road_i)
coordinates(road_i) %>% lapply(print("a"))
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
points_list <- coordinates(road_i)[[1]] %>%
lapply(as.data.frame)
a <- coordinates(road_i)
length(a)
coordinates(road_i)[[1]]
# Somtimes segment consistens of multiple broken segments. Create a list
# which contains a dataframe for each
coordinates(road_i) %>%
lapply(function(df_i) coordinates(df)[[1]])
coordinates(road_i)
